/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ro.hex.remocadm.telnet.commands;

import java.io.IOException;
import java.util.Properties;
import java.util.StringTokenizer;
import ro.hex.remocadm.telnet.TelnetNotifier;
import ro.hex.remocadm.telnet.TelnetThread;

/**
 * The class executes the 'show media' command in VLC and looks for the last
 * 'dshow-tuner-channel' in the output. If no channel is found, value -1 is
 * returned.
 *
 * @author Iulian Virtejanu
 */
public class GetVlChannelCommand extends TelnetThread {
    
    private String lastChannel = null;

    public GetVlChannelCommand(TelnetNotifier parent, Properties context) {
        super(parent, context);
    }

    @Override
    public void telnetDialog() throws IOException {
        writeLine("show media");
        String media = readUntil("> ");

        // search the last line with dshow-tuner-channel
        String lastChannelString = "";
        StringTokenizer st = new StringTokenizer(media, "\n");
        while (st.hasMoreTokens()) {
            String line = st.nextToken();
            line = line.trim();

            if (line.indexOf("v4l2-tuner-frequency") > -1) {
                int ie = line.indexOf('=');
                lastChannelString = line.substring(ie + 1);
            }
        }
        
        lastChannel = lastChannelString.substring(0, 3) + "." + lastChannelString.substring(3, 5);
    }

    /**
     * Returns last channel found.
     * @return
     */
    public String getLastChannel() {
        return lastChannel;
    }
}
